﻿using System;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using HyCommonTableTool;
using HyMaterialStatistic;

namespace ElectricMaterialStatistic
{
	public class ElectricGridManager : MepGridManagerParent
	{
		public override bool DrawGridToRevit(ExternalCommandData commandData, DlgSpecialtyStatisticTable dlgMain, DlgMaterialStatisticParent dlgFilterParent)
		{
			DlgElectircFilter dlgFilter = dlgFilterParent as DlgElectircFilter;
            FlexCell.Grid grid = new FlexCell.Grid();
			Document document = commandData.Application.ActiveUIDocument.Document;
			bool result;
			try
			{
				if (!this.InitailGridData(document, dlgMain, dlgFilter, ref grid))
				{
					result = false;
				}
				else
				{
					CREATENEWVIEW newView;
					if (dlgMain.chkNewView.Checked)
					{
						newView = CREATENEWVIEW.NEWVIEW;
					}
					else
					{
						newView = CREATENEWVIEW.EXIST;
					}
					new GridToolExportImportRevit(commandData, grid, dlgMain.txtGridName.Text, dlgMain.m_gridSetInfo.LineWidthOut, dlgMain.m_gridSetInfo.LineWidthInner, newView).DrawInRevitView();
					result = true;
				}
			}
			catch
			{
				YJKMessageBox.Show("创建表格失败");
				result = false;
			}
			return result;
		}

		public override bool ExportToExcel(ExternalCommandData commandData, DlgSpecialtyStatisticTable dlgMain, DlgMaterialStatisticParent dlgFilterParent)
		{
			DlgElectircFilter dlgFilter = dlgFilterParent as DlgElectircFilter;
            FlexCell.Grid grid = new FlexCell.Grid();
			Document document = commandData.Application.ActiveUIDocument.Document;
			bool result;
			try
			{
				if (!this.InitailGridData(document, dlgMain, dlgFilter, ref grid))
				{
					result = false;
				}
				else
				{
					int eFileType = dlgMain.m_eFileType;
					if (eFileType != 1)
					{
						if (eFileType == 2)
						{
							new GridToolExportImportExcel(true).ExportExcel(grid, dlgMain.m_strFilePath);
						}
					}
					else
					{
						new GridToolExportImportExcel(false).ExportExcel(grid, dlgMain.m_strFilePath);
					}
					result = true;
				}
			}
			catch
			{
				YJKMessageBox.Show("创建表格失败");
				result = false;
			}
			return result;
		}

        private bool InitailGridData(Document doc, DlgSpecialtyStatisticTable dlgMain, DlgElectircFilter dlgFilter, ref FlexCell.Grid grid)
		{
			string text = Path.Combine(Product.InstallLocation, "Templates", "ElectricTempletlFile");
			text = text + "\\" + dlgMain.cmbGridType.Text + ".hymb";
			if (!grid.LoadFromXML(text))
			{
				return false;
			}
			if (dlgMain.cmbGridType.Text == "材料表")
			{
				return new ElectricMaterialList(doc, dlgFilter.lstDicElems, dlgMain.txtGridName.Text, dlgMain.m_gridSetInfo, ref grid).GetStatisticGrid();
			}
			if (dlgMain.cmbGridType.Text == "设备表")
			{
				return new ElectricEquipmentList(doc, dlgFilter.lstDicElems, dlgMain.txtGridName.Text, dlgMain.m_gridSetInfo, ref grid).GetStatisticGrid();
			}
			if (dlgMain.cmbGridType.Text == "设备材料表")
			{
				return new ElectricEquipMaterialList(doc, dlgFilter.lstDicElems, dlgMain.txtGridName.Text, dlgMain.m_gridSetInfo, ref grid).GetStatisticGrid();
			}
			return dlgMain.cmbGridType.Text == "主要设备、图形符号表" && new ElectricEquipSymbolList(doc, dlgFilter.lstDicElems, dlgMain.txtGridName.Text, dlgMain.m_gridSetInfo, ref grid).GetStatisticGrid();
		}
	}
}
